package project.dao;

import java.sql.ResultSet;
import java.util.ArrayList;

import model.pojo.Employee;
import models.util.MySqlDataAccessHelper;
public class EmployeeDAO {
	
	
	public ArrayList<Employee> getAllEmployee(String database){
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();	
		ArrayList<Employee> list =new ArrayList<Employee>();
		String sql="select IDEmployee,UserName,FullName from employee";
		try {
			mysql.open(database);
			ResultSet rs = mysql.executeQuery(sql);
			Employee pr;
			while (rs.next()) {
				pr=new Employee();
				pr.setIdemployee(rs.getString("IDEmployee"));
				pr.setUserName(rs.getString("UserName"));
				pr.setFullName(rs.getString("FullName"));
				
				list.add(pr);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				mysql.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
	
	public ArrayList<Employee> getAllEmployee1(String database,String[] strCheck){
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();	
		ArrayList<Employee> list =new ArrayList<Employee>();
		String sql="select IDEmployee,UserName,FullName from employee";
		try {
			mysql.open(database);
			ResultSet rs = mysql.executeQuery(sql);
			Employee pr;
			while (rs.next()) {
				pr=new Employee();
				pr.setIdemployee(rs.getString("IDEmployee"));
				pr.setUserName(rs.getString("UserName"));
				pr.setFullName(rs.getString("FullName"));
				
				if(strCheck!=null && strCheck.length>0){
					for(int i=0;i<strCheck.length;i++){
						if(strCheck[i].equals(rs.getString("IDEmployee"))){
							pr.setChecked(true);
						}
					}
				}
				
				list.add(pr);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				mysql.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
	
	public Employee getOneEmployee(String id,String database){
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();	
		Employee pr=new Employee();
		String sql="select IDEmployee,UserName,FullNames from employee where employee.IDEmployee='"+id+"'";
		try {
			mysql.open(database);
			ResultSet rs = mysql.executeQuery(sql);
			
			if (rs.next()) {				
				pr.setIdemployee(rs.getString("IDEmployee"));
				pr.setUserName(rs.getString("UserName"));
				pr.setFullName(rs.getString("FullName"));				
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				mysql.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return pr;
	}
	
	public String getNameEmployee(String id,String database){
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();	
		String name="";
		String sql="select employee.FullName from employee where employee.IDEmployee='"+id+"'";
		try {
			mysql.open(database);
			ResultSet rs = mysql.executeQuery(sql);
			
			if (rs.next()) {	
				name=rs.getString("FullName");				
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				mysql.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return name;
	}
	
	public Employee getOneEmployeeByName(String name,String database){
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();	
		Employee pr=new Employee();
		String sql="select IDEmployee,UserName,FullName from employee where employee.FullName='"+name+"'";
		try {
			mysql.open(database);
			ResultSet rs = mysql.executeQuery(sql);
			
			if (rs.next()) {				
				pr.setIdemployee(rs.getString("IDEmployee"));
				pr.setUserName(rs.getString("UserName"));
				pr.setFullName(rs.getString("FullName"));				
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				mysql.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return pr;
	}
}
